package br.odb.bzk3.midp;

import javax.microedition.m3g.Mesh;

import br.odb.bzk3.geometry.Color;
import br.odb.bzk3.geometry.Vec3;

/**
 * 
 * @author Daniel "Monty" Monteiro
 */
public class Sector {
	 int VisibleFaces;
	 public boolean visited;
	 public int dist;
	int[] iConnection;
	Color[] iColor;
	Mesh[] iCache;
	Vec3 iStartPoint;
	Vec3 iDimensions;
	public byte lastFrame = 0;
	 public float candelas;
//	private boolean visible = false;

	public Sector(float f, float g, float h, float i, float j, float k) {
		iColor = new Color[6];
		 candelas = 64;

		iCache = new Mesh[6];
		iConnection = new int[6];

		iStartPoint = new Vec3();
		iDimensions = new Vec3();

		iStartPoint.iX = (f);
		iDimensions.iX = (g);

		iStartPoint.iY = (h);
		iDimensions.iY = (i);

		iStartPoint.iZ = (j);
		iDimensions.iZ = (k);
		 VisibleFaces = 0;
	}

	public void SetColor(int i, int i0, int i1, int i2) {

		iColor[i] = new Color();

		if (i == 5) {
			iColor[i].r = (2 * i0) / 3;
			iColor[i].g = (2 * i1) / 3;
			iColor[i].b = (2 * i2) / 3;
		} else if (i == 4) {
			iColor[i].r = i0;
			iColor[i].g = i1;
			iColor[i].b = i2;
		} else {
			iColor[i].r = i0 / 2;
			iColor[i].g = i1 / 2;
			iColor[i].b = i2 / 2;
		}
	}

	public void SetLinks(int l0, int l1, int l2, int l3, int l4, int l5) {
		SetLink(0, l0);
		SetLink(1, l1);
		SetLink(2, l2);
		SetLink(3, l3);
		SetLink(4, l4);
		SetLink(5, l5);

		 for (int c = 0; c < 6; c++)
			 if (GetLink(c) != 0)
				 VisibleFaces++;
	}

	public void SetLink(int i, int i0) {

		 if (i0 != 0)
			 VisibleFaces++;

		iConnection[i] = i0;
	}

	public int GetLink(int i) {
		return iConnection[i];
	}

	public int locateEscapePlane(Vec3 vec) {
		if ( vec.iY >  (iStartPoint.iY + iDimensions.iY ) ) {
			return 5;
		}

		if ( vec.iY <  ( iStartPoint.iY ) ) {
			return 4;
		}
		if ( (iStartPoint.iZ + iDimensions.iZ ) < vec.iZ) {
			return 2;
		}

		if ( iStartPoint.iZ > vec.iZ ) {
			return 0;
		}

		if ( (iStartPoint.iX + iDimensions.iX ) < vec.iX) {
			return 1;
		}

		if ( iStartPoint.iX > vec.iX ) {
			return 3;
		}

		return 100;
	}

	// ---------------------------------------------------------------------------------------

	void reset() {
		 visited = false;
		 candelas = 64;
		 for (int c = 0; c < 6; c++)
		 if (iCache[c] != null && iColor[c] != null)
		 iCache[c].getVertexBuffer().setDefaultColor(getARGBColor(c));
	}

	void lit() {
		// //Android
		// float Candelas = candelas / 255.0f;
		// for (int c = 0; c < 6; c++)
		// if (this.iFaces[c] != null)
		// this.iFaces[c].setOffset(Candelas);
		// //MIDP
		 int color;
		
		 if (candelas>=255)
		 candelas=255;
		
		 float _candelas=candelas;
		 _candelas/=128.0f;
		
		 for (int c=0;c<6;c++)
		 {
		 if (iColor[c] == null)
		 continue;
		
		 color=iCache[c].getVertexBuffer().getDefaultColor();
		 color=getARGBColor((int)(iColor[c].r*_candelas),(int)(iColor[c].g*_candelas),(int)(iColor[c].b*_candelas));
		 iCache[c].getVertexBuffer().setDefaultColor(color);
		 }
	}

	public void setVisible(boolean v) {
//		visible  = v;
		for (int c = 0; c < 6; c++) {
			if (iCache[c] != null) {
				iCache[c].setRenderingEnable(v);
			}
		}
	}

	public Sector() {
		iCache = new Mesh[6];
		iColor = new Color[6];
		iConnection = new int[6];
		iStartPoint = new Vec3();
		iDimensions = new Vec3();
		 candelas = 64;
	}

	public String toString() {
		return iStartPoint.toString() + "-" + iDimensions.toString();
	}

	int getARGBColor(int i) {

		int color = 0xFF000000;
		if (iColor[i] != null) {
			color += (iColor[i].r) * 0x10000;
			color += (iColor[i].g) * 0x100;
			color += (iColor[i].b);
		}
		return color;
	}

	int getARGBColor(int i0, int i1, int i2) {
		if (i0 >= 255)
			i0 = 255;
		if (i1 >= 255)
			i1 = 255;
		if (i2 >= 255)
			i2 = 255;

		int color = 0xFF000000;
		{
			color += i0 * 0x10000;
			color += i1 * 0x100;
			color += i2;
		}
		return color;
	}

	// ------------------------------------------------------------------------------------------------------------
	public float getX0() {
		return iStartPoint.iX;
	}

	// ------------------------------------------------------------------------------------------------------------
	public float getX1() {
		return iStartPoint.iX + iDimensions.iX;
	}

	// ------------------------------------------------------------------------------------------------------------
	public float getY0() {
		return iStartPoint.iY;
	}

	// ------------------------------------------------------------------------------------------------------------
	public float getY1() {
		return iStartPoint.iY + iDimensions.iY;
	}

	// ------------------------------------------------------------------------------------------------------------
	public float getZ0() {
		return iStartPoint.iZ;
	}

	// ------------------------------------------------------------------------------------------------------------
	public float getZ1() {
		return iStartPoint.iZ + iDimensions.iZ;
	}

	// ------------------------------------------------------------------------------------------------------------
	public float getWidth() {
		return iDimensions.iX;
	}

	// ------------------------------------------------------------------------------------------------------------
	public float getLength() {
		return iDimensions.iY;
	}

	// ------------------------------------------------------------------------------------------------------------
	public float getHeight() {
		return iDimensions.iZ;
	}

	public void addCandelas(float Candelas) {
		 candelas += Candelas;
		 if (candelas >= 255)
		 candelas = 254;
	}

	public Color getColor(int index) {
		return iColor[index];

	}

//	public boolean isVisible() {
//		return visible;
//	}
}
